import cn.edu.dgut.Javabean_object.Person;
import cn.edu.dgut.Javabean_object.Person2;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * The sevelet class to insert Person into database
 */
@SuppressWarnings("Duplicates")
@WebServlet(name = "CreatePersonServlet2", urlPatterns = {"/CreatePerson2"})
public class CreatePersonServlet2 extends HttpServlet {


    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        EntityManager em = (EntityManager) request.getAttribute("_entity.manager");
        String username = request.getParameter("username");
        String name = request.getParameter("name");
        String number = request.getParameter("number");
        String email = request.getParameter("email");
        String password=request.getParameter("password");

        try {

            //Get the data from user's form
//            String id = request.getParameter("id");
            //Create a person instance out of it
            Person person = new Person(username,name, number, email,password,"xx.png",null,null,null);
            em.persist(person);


        } catch (Exception ex) {
            throw new ServletException(ex);
        }
        Map<String, String> message = new HashMap<String, String>();
        message.put("user_number", username);
        message.put("name",name);
        message.put("phone_number", number);
        message.put("email", email);

        HttpSession session = request.getSession(true);
        session.setAttribute("Login", message);

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println("利用过滤器拦截");
        response.getWriter().println("添加记录成功");
        response.sendRedirect("/userInfo.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request  servlet request
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>

}
